//
//  CantripScrollView.m
//  CantripSDK
//
//  Created by James Callender on Thu Oct 10 2002.
//  Copyright (c) 2002 James Callender. All rights reserved.
//

#import "CantripScrollView.h"


@implementation CantripScrollView

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
	NSString *resourcePath = [[NSBundle bundleWithIdentifier:@"com.solidsun.cantripsdk"] resourcePath];

	_topImage1 = [[NSImage alloc] initWithContentsOfFile:[resourcePath stringByAppendingPathComponent:@"split-open.tiff"]];
	_topImage2 = [[NSImage alloc] initWithContentsOfFile:[resourcePath stringByAppendingPathComponent:@"split-close.tiff"]];
	_bottomImage1 = [[NSImage alloc] initWithContentsOfFile:[resourcePath stringByAppendingPathComponent:@"lock-close.tiff"]];
	_bottomImage2 = [[NSImage alloc] initWithContentsOfFile:[resourcePath stringByAppendingPathComponent:@"lock-open.tiff"]];
	
	_topButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0,0.0,10.0,10.0)];
	[[_topButton cell] setBezelStyle:NSShadowlessSquareBezelStyle];
	[_topButton setImage:_topImage1];
	[_topButton setTarget:self];
	[_topButton setAction:@selector(toggleTopButtonAction:)];
	
	_bottomButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0,0.0,10.0,10.0)];
	[[_bottomButton cell] setBezelStyle:NSShadowlessSquareBezelStyle];
	[_bottomButton setImage:_bottomImage1];
	[_bottomButton setTarget:self];
	[_bottomButton setAction:@selector(toggleBottomButtonAction:)];
	
	[self addSubview:_topButton];
	[self addSubview:_bottomButton];
	
	_topState = 1;
	_bottomState = 1;
	_showTop = _showBottom = YES;
    }
    return self;
}

- (void)dealloc
{
    [_topImage1 release];
    [_topImage2 release];
    [_bottomImage1 release];
    [_bottomImage2 release];
}

- (void)setTopAction:(SEL)a
{
    _topAction = a;
}

- (void)setBottomAction:(SEL)a
{
    _bottomAction = a;
}

- (void)setTopImageOn:(NSImage *)image
{
    _topImage1 = image;
}

- (void)setTopImageOff:(NSImage *)image
{
    _topImage2 = image;
}

- (void)setBottomImageOn:(NSImage *)image
{
    _bottomImage1 = image;
}

- (void)setBottomImageOff:(NSImage *)image
{
    _bottomImage2 = image;
}

- (int)topState
{
    return _topState;
}

- (int)bottomState
{
    return _bottomState;
}

- (void)drawRect:(NSRect)rect 
{
    [super drawRect:rect];
}

- (void)tile 
{
    NSScroller *vertScroller;
    NSRect scrollerFrame, buttonFrame;

    [super tile];
	
    vertScroller = [self verticalScroller];
    scrollerFrame = [vertScroller frame];
    buttonFrame = [_topButton frame];

    if (_showTop)
	scrollerFrame.size.height -= [_topButton frame].size.height;
    if (_showBottom)
	scrollerFrame.size.height -= [_bottomButton frame].size.height;
    scrollerFrame.origin.y = [_bottomButton frame].size.height;
    [vertScroller setFrame:scrollerFrame];

    buttonFrame.origin.y = 0;
    buttonFrame.size.width = scrollerFrame.size.width + 1.0;
    buttonFrame.size.height = 17;
    buttonFrame.origin.x = scrollerFrame.origin.x;
    [_topButton setFrame:buttonFrame];

    buttonFrame.origin.y = NSMaxY(scrollerFrame);
    buttonFrame.size.width = scrollerFrame.size.width + 1.0;
    buttonFrame.size.height = 17;
    buttonFrame.origin.x = scrollerFrame.origin.x;
    [_bottomButton setFrame:buttonFrame];
}

- (void)toggleTopButtonAction:(id)sender
{
    _topState = !_topState;
    if (_topState) {
	[_topButton setImage:_topImage1];
	[[self delegate] performSelector:_topAction withObject:self];
    } else {
	[_topButton setImage:_topImage2];
	[[self delegate] performSelector:_topAction withObject:self];
    }
}

- (void)toggleBottomButtonAction:(id)sender
{
    _bottomState = !_bottomState;
    if (_bottomState) {
	[_bottomButton setImage:_bottomImage1];
	[[self delegate] performSelector:_bottomAction withObject:self];
    } else {
	[_bottomButton setImage:_bottomImage2];
	[[self delegate] performSelector:_bottomAction withObject:self];
    }
}

- (id)delegate
{
    return delegate;
}

- (void)setDelegate:(id)d
{
    delegate = d;
}

@end
